package com.etc.controller;

import com.etc.entity.Userinfo;
import com.etc.service.UserinfoServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping("/userinfo")
@SessionAttributes(value = "userinfo")
public class UserinfoContoller {
    @Autowired
    UserinfoServiceImpl userinfoServiceImpl;

    /**
     * 注册
     * @param user_acount 账号
     * @param user_password 密码
     * @param user_tel 电话
     * @param user_img 头像
     * @param session session
     * @return String
     */
    @RequestMapping("/register")
    public String register(String user_acount, String user_password, String user_tel, MultipartFile user_img, HttpSession session) {
        if (!user_img.isEmpty()) {
            String filename = user_img.getOriginalFilename();
            String fileRealName = UUID.randomUUID() + filename;
            File file = new File("E:\\fileup\\" + fileRealName);
            boolean b = userinfoServiceImpl.register(new Userinfo(user_acount, user_tel, user_password, fileRealName));
            if (b) {
                try {
                    user_img.transferTo(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Userinfo u = userinfoServiceImpl.login(user_acount, user_password);
                if (u != null) {
                    session.setAttribute("userinfo", u);
                }
            }
        }
        return "forward:/index.jsp";
    }

    /**
     * 登录
     * @param user_acount 账号
     * @param user_password 密码
     * @param session session
     * @return String
     */
    @RequestMapping("/login")
    public String login(@RequestParam("user_acount") String user_acount, @RequestParam("user_password") String user_password, HttpSession session) {
        Userinfo userinfo = userinfoServiceImpl.login(user_acount, user_password);
        if (userinfo != null) {
            session.setAttribute("userinfo", userinfo);
        }
        return "forward:/index.jsp";
    }

    /**
     * 退出
     * @param status session
     * @return String
     */
    @RequestMapping("/exit")
    public String exit(SessionStatus status) {
        status.setComplete();
        return "forward:/index.jsp";
    }

    /**
     * 删除
     * @param user_no 用户编号
     * @return boolean
     */
    @RequestMapping("/deleteByUser")
    @ResponseBody
    public boolean deleteByUser(Integer user_no) {
        boolean b = userinfoServiceImpl.deleteByUser(user_no);
        return b;
    }

    /**
     * 更新
     * @param user_no 编号
     * @param user_acount 账号
     * @param user_password 密码
     * @param user_tel 电话
     * @param user_img 图片
     * @param session session
     * @return String
     * @throws IOException 异常
     */
    @RequestMapping("/updateByUser")
    public String updateByUser(Integer user_no, String user_acount, @RequestParam("user_password1") String user_password, @RequestParam("user_tel1") String user_tel, MultipartFile user_img, HttpSession session) throws IOException {
        if (!user_img.isEmpty()) {
            String filename = user_img.getOriginalFilename();
            String fileRealName = UUID.randomUUID() + filename;
            File file = new File("E:\\fileup\\" + fileRealName);
            user_img.transferTo(file);
            boolean b = userinfoServiceImpl.updateByUser(new Userinfo(user_no, user_acount, user_tel, user_password, fileRealName));
            if (b) {
                Userinfo u = userinfoServiceImpl.login(user_acount, user_password);
                session.setAttribute("userinfo", u);
            }
        } else {
            boolean b = userinfoServiceImpl.updateByUser(new Userinfo(user_no, user_acount, user_tel, user_password));
            if (b) {
                Userinfo u = userinfoServiceImpl.login(user_acount, user_password);
                session.setAttribute("userinfo", u);
            }
        }
        return "forward:/index.jsp";
    }

    /**
     * 查询所有
     * @param pageNum 页数
     * @param pageSize 数量
     * @return pageinfo
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public PageInfo<Userinfo> queryAll(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        PageInfo<Userinfo> pageInfo = userinfoServiceImpl.queryAll(pageNum, pageSize);
        return pageInfo;
    }

    /**
     * 检测账号
     * @param user_acount 账号
     * @return userinfo
     */
    @RequestMapping("/queryBy")
    @ResponseBody
    public Userinfo queryBy(String user_acount) {
        Userinfo userinfo = userinfoServiceImpl.queryBy(user_acount);
        return userinfo;
    }
}
